﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace workshop3
{
    public partial class FormGrade : Form
    {
        DataTable grade = new DataTable();
        DataColumn name = new DataColumn("Name");
        DataColumn persentage = new DataColumn("Persentage");
        //DataColumn criteria = new DataColumn("Criteria");
        DataColumn A = new DataColumn("A");
        DataColumn B = new DataColumn("B");
        DataColumn C = new DataColumn("C");
        DataColumn D = new DataColumn("D");
        DataColumn F = new DataColumn("F");

        DataTable student = new DataTable();
        DataColumn studentname = new DataColumn();
        DataColumn studentidwtf2 = new DataColumn();

        //grade.Columns.Add(persentage);
        //dt0.Columns.Add(dc3);

        int studentnum;
        int qn;
        int num;
        int currqr;
        string year;
        //string course;
        string semester;
        string taskidwtf;
        //string rubric;
        string courseid;
        string rubricid;
        string studentidwtf;
        string groupname;
        string studentfile;
        int[] question = new int[100] ;
        int[] qrubric = new int[100];
        double[] qgrade = new double[30];
        double[] total = new double[100];
        int[] check = new int[30];
        Boolean set = false;
        Boolean qadd = false;

        List<string> yearlist = new List<string>();
        List<string> semelist = new List<string>();
        // List<string> courselist = new List<string>();
        List<string> tasklist = new List<string>();
        List<string> rubriclist = new List<string>();
        List<string> questionlist = new List<string>();
        List<string> delaylist = new List<string>();
        List<string> grouplist = new List<string>();

        public FormGrade()
        {
            InitializeComponent();
        }

        private void FormGrade_Load(object sender, EventArgs e)
        {
            qn = 1;
            txt_qn.Text = qn.ToString();

            grade.Columns.Add(name);
            grade.Columns.Add(persentage);
            grade.Columns.Add(A);
            grade.Columns.Add(B);
            grade.Columns.Add(C);
            grade.Columns.Add(D);
            grade.Columns.Add(F);

            student.Columns.Add(studentname);
            student.Columns.Add(studentidwtf2);


            delaylist.Add("no overdue");//add the data sourse for overdue operation
            delaylist.Add("1 day");
            delaylist.Add("2 days");
            delaylist.Add("3 days");
            delaylist.Add("more than 3 days");

            Gdelay.DataSource = delaylist;
            Gdelay.Enabled = false;

            for (int i = 0; i < 100; i++) {
                qrubric[i] = -1;
            }

            for (int i = 0; i < 100; i++)
                total[i] = -1;


            if (File.Exists("rubric.xml") && File.Exists("course.xml"))//add data sourse for course and semester drop-down box
            {
                XmlDocument coursedoc = new XmlDocument();
                coursedoc.Load("course.xml");
                string coursepath = "//course";
                XmlNodeList coursenodelist = coursedoc.SelectNodes(coursepath);

                if (coursenodelist != null)
                {
                    for (int i = 0; i < coursenodelist.Count; i++)
                    {
                        if (yearlist.Count == 0 && semelist.Count == 0)
                        {
                            //courselist.Rows.Add(coursenodelist[i].Attributes[0].Value.ToString(),coursenodelist[i].ChildNodes[0].InnerText);
                            yearlist.Add(coursenodelist[i].ChildNodes[1].InnerText);
                            semelist.Add(coursenodelist[i].ChildNodes[2].InnerText);
                        }
                        else
                        {
                            if (yearlist.Contains(coursenodelist[i].ChildNodes[1].InnerText))
                            {
                                //continue;
                            }
                            else
                            {
                                yearlist.Add(coursenodelist[i].ChildNodes[1].InnerText);
                            }
                            if (semelist.Contains(coursenodelist[i].ChildNodes[2].InnerText))
                            {
                                //continue;
                            }
                            else
                            {
                                semelist.Add(coursenodelist[i].ChildNodes[2].InnerText);
                            }
                        }
                    }

                    Gyear.DataSource = yearlist;
                    Gsemester.DataSource = semelist;

                }
            }
                /*else
                {
                    MessageBox.Show("file empty!");
                }
            }
            else
            {
                MessageBox.Show("file not exist!");
            }
            }*/
        }

        private void Gyear_SelectedValueChanged(object sender, EventArgs e)//when year box changed, reload the data set of  relative course
        {
            //courselist.Clear();
            tasklist.Clear();
            Gtask.DataSource = null;
            taskidwtf = null;

            DataTable dt0 = new DataTable();
            DataColumn dc0 = new DataColumn();
            DataColumn dc1 = new DataColumn();
            dt0.Columns.Add(dc0);
            dt0.Columns.Add(dc1);

            Gcourse.DataSource = null;
            year = Gyear.SelectedValue.ToString();
            if (year != null && semester != null)
            {
                if (File.Exists("rubric.xml") && File.Exists("course.xml"))
                {
                    XmlDocument coursedoc1 = new XmlDocument();
                    coursedoc1.Load("course.xml");
                    string coursepath = "//course";
                    XmlNodeList coursenodelist = coursedoc1.SelectNodes(coursepath);
                    if (coursenodelist.Count != 0)
                    {
                        foreach (XmlNode xn in coursenodelist)
                        {
                            if (year == xn.ChildNodes[1].InnerText && xn.ChildNodes[2].InnerText == semester)
                            {
                                //courselist.Add(xn.ChildNodes[0].InnerText);
                                dt0.Rows.Add(xn.ChildNodes[0].InnerText, xn.Attributes[0].InnerText);
                            }
                        }
                    }
                }
                //Gcourse.DataSource = courselist;
                Gcourse.DataSource = dt0;
                Gcourse.DisplayMember = dt0.Columns[0].ToString();
                Gcourse.ValueMember = dt0.Columns[1].ToString();
            }
           
        }

        private void Gcourse_SelectedValueChanged(object sender, EventArgs e)
        {//when course drop-down box changed, reload the task and rubric information(data set)
            tasklist.Clear();
            Gtask.DataSource = null;
            Grubric.DataSource = null;
            taskidwtf = null;
            int whatever;

            DataTable dt0 = new DataTable();
            DataColumn dc0 = new DataColumn();
            DataColumn dc1 = new DataColumn();
            dt0.Columns.Add(dc0);
            dt0.Columns.Add(dc1);

            DataTable tasktable = new DataTable();
            DataColumn taskname = new DataColumn();
            DataColumn taskid = new DataColumn();
            tasktable.Columns.Add(taskname);
            tasktable.Columns.Add(taskid);

            if (Gcourse.DataSource != null)// load the task data set
            {
                courseid = Gcourse.SelectedValue.ToString();
                if (year != null && semester != null && courseid != null)
                {
                    if (File.Exists("rubric.xml") && File.Exists("course.xml"))
                    {
                        XmlDocument taskdoc1 = new XmlDocument();
                        taskdoc1.Load("course.xml");
                        string coursepath = "//courses/course";
                        XmlNodeList tasknodelist = taskdoc1.SelectNodes(coursepath);
                        if (tasknodelist.Count != 0)
                        {
                            for (int i = 0; i < (tasknodelist.Count); i++)
                            {
                                if (tasknodelist[i].ChildNodes[1].InnerText == year && tasknodelist[i].ChildNodes[2].InnerText == semester && tasknodelist[i].Attributes[0].InnerText == courseid)
                                {
                                    for (int k = 4; k < tasknodelist[i].ChildNodes.Count-1; k++)
                                    {
                                        //tasklist.Add(tasknodelist[i].ChildNodes[k].InnerText);
                                        tasktable.Rows.Add(tasknodelist[i].ChildNodes[k].InnerText, tasknodelist[i].ChildNodes[k].Attributes[0].InnerText);
                                    }
                                    //int.TryParse(tasknodelist[i].ChildNodes.Count.ToString(), out whatever);
                                    //if()
                                    studentfile = tasknodelist[i].ChildNodes[tasknodelist[i].ChildNodes.Count-1].InnerText;
                                }

                            }
                            XmlDocument rubricdoc1 = new XmlDocument();// load rubric dataset
                            rubricdoc1.Load("rubric.xml");
                            string rubpath = "//rubrics/rubric";
                            XmlNodeList rubnodelist = rubricdoc1.SelectNodes(rubpath);
                            if (rubnodelist.Count != 0)
                            {
                                for (int i = 0; i < rubnodelist.Count; i++)
                                {
                                    if (courseid == rubnodelist[i].Attributes[2].InnerText)
                                    {
                                        //XmlNodeList item = rubnodelist[i].ChildNodes;
                                        // foreach (XmlNode xn in item) {
                                        //    grade.Rows.Add(xn.Attributes[0].Value, xn.Attributes[1].Value);
                                        //}
                                        //rubriclist.Add(rubnodelist[i].Attributes[1].InnerText);
                                        dt0.Rows.Add(rubnodelist[i].Attributes[1].InnerText,rubnodelist[i].Attributes[0].InnerText);
                                    }
                                }

                            }
                        }

                    }
                    Gtask.DataSource = tasktable;
                    Gtask.DisplayMember = tasktable.Columns[0].ToString();
                    Gtask.ValueMember = tasktable.Columns[1].ToString();
                    Grubric.DataSource = dt0;
                    Grubric.DisplayMember = dt0.Columns[0].ToString();
                    Grubric.ValueMember = dt0.Columns[1].ToString();
                }
                else {
                    courseid = null;
                    rubricid = null;
                }
            }
        }

        private void Gsemester_SelectedValueChanged(object sender, EventArgs e)
        {// same as Gyear_SelectedValueChanged(), change the course data set after user change semester value
            //courselist.Clear();
            tasklist.Clear();
            Gcourse.DataSource = null;
            taskidwtf = null;

            DataTable dt0 = new DataTable();
            DataColumn dc0 = new DataColumn();
            DataColumn dc1 = new DataColumn();
            dt0.Columns.Add(dc0);
            dt0.Columns.Add(dc1);

            semester = Gsemester.SelectedValue.ToString();
            if (year != null && semester != null)
            {
                if (File.Exists("rubric.xml") && File.Exists("course.xml"))
                {
                    XmlDocument coursedoc1 = new XmlDocument();
                    coursedoc1.Load("course.xml");
                    string coursepath = "//course";
                    XmlNodeList coursenodelist = coursedoc1.SelectNodes(coursepath);
                    if (coursenodelist.Count != 0)
                    {
                        foreach (XmlNode xn in coursenodelist)
                        {
                            if (year == xn.ChildNodes[1].InnerText && xn.ChildNodes[2].InnerText == semester)
                            {
                                //courselist.Add(xn.ChildNodes[0].InnerText);
                                dt0.Rows.Add(xn.ChildNodes[0].InnerText,xn.Attributes[0].InnerText);
                            }
                        }
                    }
                }
                //Gcourse.DataSource = courselist;
                Gcourse.DataSource = dt0;
                Gcourse.DisplayMember = dt0.Columns[0].ToString();
                Gcourse.ValueMember = dt0.Columns[1].ToString();
            }
            
        }

        private void Gtask_SelectedValueChanged(object sender, EventArgs e)
        {//get the task value that user choose
            if(Gtask.DataSource != null && Gtask.SelectedValue != null)
                taskidwtf = Gtask.SelectedValue.ToString();
        }

        private void btn_addQuestion_Click(object sender, EventArgs e)
        {// add new questin for the task
            int.TryParse(txt_persentage.Text.ToString(), out question[qn-1]);
            qn++;
            txt_qn.Text = qn.ToString();
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {// compute the total persentage of the task question
            //and disable the drop-down box of course and task
            int.TryParse(txt_persentage.Text.ToString(), out question[qn-1]);

            int sum = 0;
            for (int i = 0; i < 100; i++) {
                if (question[i] != 0)
                {
                    sum = sum + question[i];
                    questionlist.Add((i+1).ToString());
                }
            }
            if (sum != 100)
            {
                MessageBox.Show("Wrong persentage!");
            }
            else {
                Gquestion.DataSource = questionlist;
                btn_addQuestion.Enabled = false;
                Gtask.Enabled = false;
                Gcourse.Enabled = false;
            }
        }

        private void Grubric_SelectedValueChanged(object sender, EventArgs e)
        {//load the datagridview to show the detail info of rubric
            grade.Clear();
            num = 0;
            if (Grubric.DataSource != null && Grubric.SelectedValue != null)
            {
                rubricid = Grubric.SelectedValue.ToString();

                

                DataTable dt0 = new DataTable();
                DataColumn dc0 = new DataColumn();
                DataColumn dc1 = new DataColumn();
                DataColumn dc2 = new DataColumn();
                DataColumn dc3 = new DataColumn();
                DataColumn dc4 = new DataColumn();
                DataColumn dc5 = new DataColumn();
                DataColumn dc6 = new DataColumn();
                dt0.Columns.Add(dc0);
                dt0.Columns.Add(dc1);
                dt0.Columns.Add(dc2);
                dt0.Columns.Add(dc3);
                dt0.Columns.Add(dc4);
                dt0.Columns.Add(dc5);
                dt0.Columns.Add(dc6);

                if (File.Exists("rubric.xml"))// get criterial of the chosen rubric
                {
                    XmlDocument rubricdoc1 = new XmlDocument();
                    rubricdoc1.Load("rubric.xml");
                    string rubricpath = "//rubrics/rubric";
                    XmlNodeList rubricnodelist = rubricdoc1.SelectNodes(rubricpath);
                    if (rubricnodelist.Count != 0)
                    {
                        for (int i = 0; i < rubricnodelist.Count; i++)
                        {
                            if (rubricnodelist[i].Attributes[0].InnerText == rubricid && rubricnodelist[i].Attributes[2].InnerText == courseid)
                            {
                                XmlNodeList item = rubricnodelist[i].ChildNodes;
                                foreach (XmlNode xn in item)
                                {
                                    grade.Rows.Add(xn.Attributes[1].Value, xn.InnerText,"        ", "        ", "        ", "        ", "        ");
                                    num++;
                                }
                            }

                        }
                        //dgvrubric.DataSource = grade;
                        if (currqr != 0)
                        {
                            dgvrubric.DataSource = grade;
                        }
                        //dgvrubric.Columns[2].DefaultCellStyle.Font = new Font(10);
                    }
                }
                
            }
           // MessageBox.Show("1" + num.ToString());
        }

        

        private void Gquestion_SelectedValueChanged(object sender, EventArgs e)
        {//get the question number that user chosen, and load the corresponding rubric criterial info

            // int qr;
            //string questionNum = Gquestion.SelectedValue.ToString();
            //int.TryParse(questionNum, out qr);
            grade.Clear();
            currqr = -1;
            num = 0;
            //int currqr;
            if(Gquestion.DataSource != null)
            int.TryParse( Gquestion.SelectedValue.ToString(),out currqr);

            if (File.Exists("rubric.xml")&& currqr != -1)
            {
                XmlDocument rubricdoc2 = new XmlDocument();
                rubricdoc2.Load("rubric.xml");
                string rubricpath = "//rubrics/rubric";
                XmlNodeList rubricnodelist1 = rubricdoc2.SelectNodes(rubricpath);
                if (rubricnodelist1.Count != 0)
                {
                    for (int i = 0; i < rubricnodelist1.Count; i++)
                    {
                        if (rubricnodelist1[i].Attributes[0].InnerText == qrubric[currqr-1].ToString() && rubricnodelist1[i].Attributes[2].InnerText == courseid)
                        {
                            XmlNodeList item = rubricnodelist1[i].ChildNodes;
                            foreach (XmlNode xn in item)
                            {
                                grade.Rows.Add(xn.Attributes[1].Value, xn.InnerText, "        ", "        ", "        ", "        ", "        ");
                                num++;
                            }
                        }

                    }
                    //dgvrubric.DataSource = grade;
                    dgvrubric.DataSource = grade;
                    //dgvrubric.Columns[2].DefaultCellStyle.Font = new Font(10);
                }
            }
           // MessageBox.Show("2" + num.ToString());

        }

        private void btn_confirmQR_Click(object sender, EventArgs e)
        {// get the question number and rubric that user select, record the corresponding relation for them
            // and check if user select every question correspond rubric
            int qr;
            string questionNum = Gquestion.SelectedValue.ToString();
            int.TryParse(questionNum, out qr);
            string rubricwtf = Grubric.SelectedValue.ToString();
            int.TryParse(rubricwtf, out qrubric[qr-1]);

            for (int n = 0; n < qn; n++) {
                if (qrubric[n] == -1)
                    break;
                if (n == (qn - 1)) {
                    set = true;
                    radioBtn_individual.Enabled = true;
                    radioBtn_group.Enabled = true;
                    Grubric.Enabled = false;
                    btn_confirm.Enabled = false;
                    btn_confirmQR.Enabled = false;
                    Grubric.DataSource = null;
                }
            }
        }

        private void radioBtn_individual_CheckedChanged(object sender, EventArgs e)
        {// for user select grade for individual student or group
            //if it is individual, load all the student in the course and show on Gstudent drop-down box

            //MessageBox.Show("try");
            Ggroup.Enabled = false;
            studentnum = 0;
            radioBtn_group.Enabled = false;
            DataTable dt0 = new DataTable();
            DataColumn dc0 = new DataColumn();
            DataColumn dc1 = new DataColumn();
            dt0.Columns.Add(dc0);
            dt0.Columns.Add(dc1);

            studentfile = studentfile + ".xml";

            if (File.Exists(studentfile)) {
                XmlDocument studentdoc1 = new XmlDocument();
                studentdoc1.Load(studentfile);
                string path = "//Students/student";
                XmlNodeList studentnodelist = studentdoc1.SelectNodes(path);
                if (studentnodelist.Count != 0) {
                    for (int i = 0; i < studentnodelist.Count; i++) {

                        student.Rows.Add(studentnodelist[i].ChildNodes[1].InnerText, studentnodelist[i].ChildNodes[0].InnerText);
                        studentnum++;
                    }

                }
                Gstudent.DataSource = student;
                Gstudent.DisplayMember = student.Columns[0].ToString();
                Gstudent.ValueMember = student.Columns[1].ToString();
            }
        }

        private void radioBtn_group_CheckedChanged(object sender, EventArgs e)
        {
            //if it is group, load all the group name from the student database of this course

            radioBtn_individual.Enabled = false;
            Gstudent.Enabled = false;
            //List<string> grouplist = new List<string>();

            studentfile = studentfile + ".xml";

            if (File.Exists(studentfile)) {
                XmlDocument studentdoc2 = new XmlDocument();
                studentdoc2.Load(studentfile);
                string path = "//Students/student";
                XmlNodeList studentnodelist2 = studentdoc2.SelectNodes(path);
                if (studentnodelist2.Count != 0)
                {
                    for (int i = 0; i < studentnodelist2.Count; i++)
                    {
                        if (grouplist.Count == 0)
                        {
                            grouplist.Add(studentnodelist2[i].ChildNodes[2].InnerText);

                        }
                        else if (grouplist.Contains(studentnodelist2[i].ChildNodes[2].InnerText))
                        {

                        }
                        else {

                            grouplist.Add(studentnodelist2[i].ChildNodes[2].InnerText);

                        }

                    }
                    
                }
                Ggroup.DataSource = grouplist;
            }
        }

        private void dgvrubric_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //user can grade question or rubric in click the corresponding column
            //this is for check which column user click, and compute the grade for each criterial

            //MessageBox.Show(e.RowIndex.ToString()+":::"+e.ColumnIndex.ToString());
            Gquestion.Enabled = false;
            Gstudent.Enabled = false;
            Gdelay.Enabled = true;
            //Ggroup.Enabled = true;
            double.TryParse(dgvrubric.Rows[e.RowIndex].Cells[1].Value.ToString(), out qgrade[e.RowIndex]);
            if (e.ColumnIndex == 2)
                qgrade[e.RowIndex] = qgrade[e.RowIndex] * 1;
            if(e.ColumnIndex == 3)
                qgrade[e.RowIndex] = qgrade[e.RowIndex] * 0.75;
            if (e.ColumnIndex == 4)
                qgrade[e.RowIndex] = qgrade[e.RowIndex] * 0.5;
            if (e.ColumnIndex == 5)
                qgrade[e.RowIndex] = qgrade[e.RowIndex] * 0.25;
            if (e.ColumnIndex == 6)
                qgrade[e.RowIndex] = qgrade[e.RowIndex] * 0;
            
            //dgvrubric.SelectedRows[0].DefaultCellStyle.BackColor = Color.White;
            dgvrubric.Rows[e.RowIndex].Cells[2].Style.BackColor = Color.White;
            dgvrubric.Rows[e.RowIndex].Cells[3].Style.BackColor = Color.White;
            dgvrubric.Rows[e.RowIndex].Cells[4].Style.BackColor = Color.White;
            dgvrubric.Rows[e.RowIndex].Cells[5].Style.BackColor = Color.White;
            dgvrubric.Rows[e.RowIndex].Cells[6].Style.BackColor = Color.White;
            dgvrubric.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Green;//the selected cell will turn green, remind user which grade he selected
            MessageBox.Show(qgrade[e.RowIndex].ToString());
            check[e.RowIndex] = 1;
        }

        private void btn_confirmgrade_Click(object sender, EventArgs e)
        {//compute grade for each question
            //if all the question has been graded, than will compute the final grade for this task, than store in database
           

            //Gquestion.Enabled = true;
            int delay;// get the overdue info
            if (Gdelay.SelectedValue.ToString() == "no overdue")
            {
                delay = 100;

            }
            else if (Gdelay.SelectedValue.ToString() == "1 day")
            {
                delay = 80;
            }
            else if (Gdelay.SelectedValue.ToString() == "2 days")
            {
                delay = 60;
            }
            else if (Gdelay.SelectedValue.ToString() == "3 days")
            {
                delay = 40;
            }
            else {
                delay = 0;
            }


            Boolean check1 = false;
            Boolean check2 = false;
            double sumGrade = 0;
            if (taskidwtf != null) {
                if (set == true) {
                    //MessageBox.Show(num.ToString());
                    for (int i = 0; i < num; i++)// chech whether all criterials have been graded
                    {
                        if (check[i] == 0)
                            break;
                        if (i == (num - 1))
                            check1 = true;
                    }
                    if (check1 == true)// if all criterials graded, compute the current question grade
                    {
                        Gquestion.Enabled = true;
                        Gdelay.Enabled = false;
                        for (int n = 0; n < num; n++)
                        {
                            sumGrade = sumGrade + qgrade[n];

                        }
                        if(check2 == false)
                            MessageBox.Show(currqr.ToString()+":"+sumGrade.ToString());
                        total[currqr - 1] = sumGrade;
                        for (int i = 0; i < questionlist.Count; i++) {
                            if (questionlist[i] == Gquestion.SelectedValue.ToString())
                            {
                                questionlist.Remove(questionlist[i]);
                                                               
                            }
                            //Gquestion.Enabled = false;
                        }
                        Gquestion.DataSource = null;
                        Gquestion.DataSource = questionlist;

                        if (courseid != null && rubricid != null)
                        {//chech whether all the tasks have been graded
                            for (int i = 0; i < qn ; i++) {

                                if (total[i] == -1)
                                    break;
                                if (i == (qn - 1))
                                    check2 = true;

                            }
                            if (check2 == true)//if all the tasks graded 
                            {
                                Gdelay.Enabled = true;
                                if (groupname == null && studentidwtf != null)//if groupname is null, that mean user choose to grade for individual
                                {
                                    //Gquestion.Enabled = true;
                                    Gstudent.Enabled = true;
                                    qadd = true;
                                    total[qn] = 0;
                                    for (int i = 0; i < qn; i++)
                                        total[qn] = total[qn] + total[i] / 100 * question[i];
                                    total[qn] = total[qn] * delay / 100;
                                    MessageBox.Show(total[qn].ToString());
                                    //need courseName, courseid, taskName, studentName, studentid, total[qn]
                                    if (File.Exists("grade.xml") == false)
                                    {
                                        //if there is not grade.xml, than create one, and than store the info

                                        XmlDocument xmldoc = new XmlDocument();
                                        XmlNode node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "");
                                        xmldoc.AppendChild(node);
                                        //XmlDocument xmldoc = new XmlDocument();
                                        //xmldoc.Load("grade.xml");
                                        XmlNode node1 = xmldoc.CreateElement("Grades");
                                        XmlNode root = xmldoc.CreateElement("grade");
                                        root.Attributes.Append(CreateAttribute(root, "courseid", courseid));
                                        //root.Attributes.Append(CreateAttribute(root,""))
                                        xmldoc.AppendChild(node1);
                                        node1.AppendChild(root);

                                        XmlElement task = xmldoc.CreateElement("task");
                                        task.Attributes.Append(CreateAttribute(task, "id", taskidwtf));
                                        root.AppendChild(task);
                                        XmlElement student = xmldoc.CreateElement("student");
                                        student.Attributes.Append(CreateAttribute(student, "studentid", studentidwtf));
                                        student.InnerText = total[qn].ToString();
                                        task.AppendChild(student);

                                        XmlDocument coursedoc2 = new XmlDocument();
                                        coursedoc2.Load("course.xml");
                                        string coursepath = "//course";
                                        XmlNodeList coursenodelist = coursedoc2.SelectNodes(coursepath);
                                        for (int i = 0; i < coursenodelist.Count; i++) {
                                            if (coursenodelist[i].Attributes[0].InnerText != courseid)
                                            {
                                                XmlNode root1 = xmldoc.CreateElement("grade");
                                                root1.Attributes.Append(CreateAttribute(root1, "courseid", coursenodelist[i].Attributes[0].InnerText));
                                                node1.AppendChild(root1);
                                            }
                                        }

                                        xmldoc.Save("grade.xml");

                                    }
                                    else// if exist grade.xml, load and store info in it
                                    {
                                        XmlDocument xmldoc = new XmlDocument();
                                        xmldoc.Load("grade.xml");
                                        string path = "//Grades/grade[@courseid ='" + courseid + "']";
                                        //XmlNode root = xmldoc.SelectSingleNode(path);
                                        XmlNode grade = xmldoc.SelectSingleNode(path);
                                        if (grade != null)
                                        {
                                            for (int i = 0; i < grade.ChildNodes.Count; i++)
                                            {
                                                if (grade.ChildNodes[i].Attributes[0].InnerText == taskidwtf)
                                                {
                                                    XmlElement student = xmldoc.CreateElement("student");
                                                    student.Attributes.Append(CreateAttribute(student, "studentid", studentidwtf));
                                                    student.InnerText = total[qn].ToString();
                                                    grade.ChildNodes[i].AppendChild(student);
                                                }
                                                if (i == (grade.ChildNodes.Count - 1))//if this is the first time to grade this task, create node for this new task
                                                {
                                                    if (grade.ChildNodes[i].Attributes[0].InnerText != taskidwtf)
                                                    {
                                                        XmlElement task = xmldoc.CreateElement("task");
                                                        task.Attributes.Append(CreateAttribute(task, "id", taskidwtf));
                                                        grade.AppendChild(task);
                                                        XmlElement student = xmldoc.CreateElement("student");
                                                        student.Attributes.Append(CreateAttribute(student, "studentid", studentidwtf));
                                                        student.InnerText = total[qn].ToString();
                                                        task.AppendChild(student);
                                                        break;
                                                    }


                                                }


                                            }

                                            xmldoc.Save("grade.xml");

                                        }




                                    }
                                    
                                    //clear the data after one task have been computed and stored
                                    for (int i = 0; i < qn; i++)
                                        total[i] = -1;
                                    total[qn] = -1;
                                    Gstudent.DataSource = null;
                                    //student.Select("column1 = '" + studentidwtf + "'");

                                    //delete the student that has been graded from Gstudent drop-down box
                                    for (int i = 0; i < student.Rows.Count; i++)
                                    {
                                        if (student.Rows[i][1].ToString() == studentidwtf)
                                        {
                                            student.Rows.Remove(student.Rows[i]);
                                            studentnum--;
                                        }

                                    }

                                    Gstudent.DataSource = student;
                                    Gstudent.DisplayMember = student.Columns[0].ToString();
                                    Gstudent.ValueMember = student.Columns[1].ToString();


                                }//here here

                                else {// if user choose grade for group
                                    MessageBox.Show("group start");
                                    //studentfile = studentfile + ".xml";
                                    Ggroup.Enabled = true;
                                    qadd = true;
                                    total[qn] = 0;
                                    for (int i = 0; i < qn; i++)
                                        total[qn] = total[qn] + total[i] / 100 * question[i];
                                    total[qn] = total[qn] * delay / 100;
                                    MessageBox.Show(total[qn].ToString());
                                    if (File.Exists("grade.xml") == false)// if it is the first time to grade, create xml
                                    {
                                        XmlDocument xmldoc = new XmlDocument();
                                        XmlNode node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "");
                                        xmldoc.AppendChild(node);
                                        //XmlDocument xmldoc = new XmlDocument();
                                        //xmldoc.Load("grade.xml");
                                        XmlNode node1 = xmldoc.CreateElement("Grades");
                                        XmlNode root = xmldoc.CreateElement("grade");
                                        root.Attributes.Append(CreateAttribute(root, "courseid", courseid));
                                        //root.Attributes.Append(CreateAttribute(root,""))
                                        xmldoc.AppendChild(node1);
                                        node1.AppendChild(root);

                                        XmlElement task = xmldoc.CreateElement("task");
                                        task.Attributes.Append(CreateAttribute(task, "id", taskidwtf));
                                        root.AppendChild(task);

                                        XmlDocument xmldoc2 = new XmlDocument();//load student xml to get all the student that in this group
                                        xmldoc2.Load(studentfile);
                                        string path = "//Students/student";
                                        XmlNodeList studentnodelist3 = xmldoc2.SelectNodes(path);
                                        if (studentnodelist3.Count != 0)
                                        {
                                            for (int i = 0; i < studentnodelist3.Count; i++)
                                            {
                                                if (studentnodelist3[i].ChildNodes[2].InnerText == groupname)//find all student in this group
                                                {
                                                    //give grade for every student in this group
                                                    XmlElement student = xmldoc.CreateElement("student");
                                                    student.Attributes.Append(CreateAttribute(student, "studentid", studentnodelist3[i].ChildNodes[0].InnerText));
                                                    student.InnerText = total[qn].ToString();
                                                    task.AppendChild(student);
                                                    xmldoc.Save("grade.xml");
                                                }
                                            }
                                        }
                                        XmlDocument coursedoc2 = new XmlDocument();
                                        coursedoc2.Load("course.xml");
                                        string coursepath = "//course";
                                        XmlNodeList coursenodelist = coursedoc2.SelectNodes(coursepath);
                                        for (int i = 0; i < coursenodelist.Count; i++)
                                        {
                                            if (coursenodelist[i].Attributes[0].InnerText != courseid)
                                            {
                                                XmlNode root1 = xmldoc.CreateElement("grade");
                                                root1.Attributes.Append(CreateAttribute(root1, "courseid", coursenodelist[i].Attributes[0].InnerText));
                                                node1.AppendChild(root1);
                                            }
                                        }
                                        xmldoc.Save("grade.xml");
                                    }
                                    else {// exist grade.xml, load it 
                                        XmlDocument xmldoc = new XmlDocument();
                                        xmldoc.Load("grade.xml");
                                        string path = "//Grades/grade[@courseid ='" + courseid + "']";
                                        //XmlNode root = xmldoc.SelectSingleNode(path);
                                        XmlNode grade = xmldoc.SelectSingleNode(path);
                                        if (grade != null)
                                        {
                                            for (int i = 0; i < grade.ChildNodes.Count; i++)
                                            {
                                                if (grade.ChildNodes[i].Attributes[0].InnerText == taskidwtf)
                                                {
                                                    
                                                    XmlDocument xmldoc2 = new XmlDocument();
                                                    xmldoc2.Load(studentfile);
                                                    string path1 = "//Students/student";
                                                    XmlNodeList studentnodelist3 = xmldoc2.SelectNodes(path1);
                                                    if (studentnodelist3.Count != 0)
                                                    {
                                                        for (int k = 0; k < studentnodelist3.Count; k++)
                                                        {
                                                            if (studentnodelist3[k].ChildNodes[2].InnerText == groupname)
                                                            {

                                                                XmlElement student = xmldoc.CreateElement("student");
                                                                student.Attributes.Append(CreateAttribute(student, "studentid", studentnodelist3[k].ChildNodes[0].InnerText));
                                                                student.InnerText = total[qn].ToString();
                                                                grade.ChildNodes[i].AppendChild(student);
                                                                xmldoc.Save("grade.xml");
                                                            }
                                                        }
                                                    }



                                                }
                                                if (i == (grade.ChildNodes.Count - 1))//if this is the first time to grade this task, create node for this new task
                                                {
                                                    if (grade.ChildNodes[i].Attributes[0].InnerText != taskidwtf)
                                                    {
                                                        XmlElement task = xmldoc.CreateElement("task");
                                                        task.Attributes.Append(CreateAttribute(task, "id", taskidwtf));
                                                        grade.AppendChild(task);
                                                        XmlDocument xmldoc2 = new XmlDocument();
                                                        xmldoc2.Load(studentfile);
                                                        string path1 = "//Students/student";
                                                        XmlNodeList studentnodelist3 = xmldoc2.SelectNodes(path1);
                                                        if (studentnodelist3.Count != 0)
                                                        {
                                                            for (int k = 0; k < studentnodelist3.Count; k++)
                                                            {
                                                                if (studentnodelist3[k].ChildNodes[2].InnerText == groupname)
                                                                {

                                                                    XmlElement student = xmldoc.CreateElement("student");
                                                                    student.Attributes.Append(CreateAttribute(student, "studentid", studentnodelist3[k].ChildNodes[0].InnerText));
                                                                    student.InnerText = total[qn].ToString();
                                                                    task.AppendChild(student);
                                                                    xmldoc.Save("grade.xml");
                                                                }
                                                            }
                                                        }
                                                        break;
                                                    }


                                                }


                                            }

                                            xmldoc.Save("grade.xml");

                                        }


                                    }
                                    for (int i = 0; i < qn; i++)
                                        total[i] = -1;
                                    total[qn] = -1;
                                    // here here
                                    for (int i = 0; i < grouplist.Count; i++) {
                                        if (grouplist[i].ToString() == groupname) {
                                            grouplist.Remove(grouplist[i]);
                                                break;
                                        }

                                    }
                                    Ggroup.DataSource = null;
                                    Ggroup.DataSource = grouplist;

                                }
                            }
                            



                        }




                        //clear all the data after grade one question
                        for (int i = 0; i < num; i++)
                            check[i] = 0;

                         
                        sumGrade = 0;
                        check1 = false;
                        check2 = false;
                        num = 0;
                        dgvrubric.DataSource = null;
                    }
                }
                
            }
        }

        private void Gstudent_SelectedValueChanged(object sender, EventArgs e)
        {//get the student info form the drop-down box
            if (Gstudent.DataSource != null)
            {
                if (studentnum > 0)
                {
                    //MessageBox.Show(studentnum.ToString());
                    studentidwtf = Gstudent.SelectedValue.ToString();
                }
                else
                    MessageBox.Show("you have graded all student!");
                //studentidwtf = Gstudent.SelectedValue.ToString();
            }
            if(qadd) { // reload the dataset for Gquestion drop-down box
                Gquestion.DataSource = null;
                for (int i = 0; i < qn; i++)
                    questionlist.Add((i + 1).ToString());
                Gquestion.DataSource = questionlist;
                qadd = false;
            }
        }

        public static XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
        {
            try
            {
                XmlDocument doc = node.OwnerDocument;       //createa new xmldocument class which is our own document   
                XmlAttribute attr = doc.CreateAttribute(attributeName);
                attr.Value = value;     //input the value in it
                node.Attributes.SetNamedItem(attr);
                return attr;
            }
            catch (Exception err)
            {
                string desc = err.Message;
                return null;
            }
        }

        private void Ggroup_SelectedValueChanged(object sender, EventArgs e)
        {// same as Gstudent
            if (Ggroup.DataSource != null) {
                groupname = Ggroup.SelectedValue.ToString();
                
            }
            if (qadd) {
                Gquestion.DataSource = null;
                for (int i = 0; i < qn; i++)
                    questionlist.Add((i + 1).ToString());
                Gquestion.DataSource = questionlist;
                qadd = false;
                
            }
        }

        private void btn_user_Click(object sender, EventArgs e)
        {// go back to management page
            Formmanagement user = new Formmanagement();
            user.Show();
            this.Hide();
        }
    }
}
